package com.ft.oa.service.industryService.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.domain.industrys.IndustryDoc;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.industrysMapper.IndustryDocMapper;
import com.ft.oa.service.industryService.IIndustryDocService;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 行业文档Service业务层处理
 *
 * @author baowz
 * @date 2021-12-30
 */
@Service
public class IndustryDocServiceImpl implements IIndustryDocService {
    @Resource
    private IndustryDocMapper industryDocMapper;
    @Resource
    private DirectoryService directoryService;
    /**
     * 获取行业领域管理详情页面
     * @author baowz
     * @date 2021-12-30
     */
    @Override
    public List<IndustryDoc> selectHyparticulars(Long hyid) {
        //行业详情id
        QueryWrapper<IndustryDoc> wrapper = new QueryWrapper();
        wrapper.eq("industry_id",hyid);
        return industryDocMapper.selectList(wrapper);
    }

    @Override
    public int docvaluenum(Long hyid, int valueid) {
        //行业详情id
        QueryWrapper<IndustryDoc> wrapper = new QueryWrapper();
        wrapper.eq("industry_id",hyid);
        wrapper.eq("valueid",valueid);
        return industryDocMapper.selectCount(wrapper);
    }

    @Override
    public int addindustryDoc(IndustryDoc industryDoc) {
        // 获取当前用户名称用作展示售前项目管理
        Long creator = SecurityUtils.getCurrentUser().getId();
        //创建人
        industryDoc.setCreator(String.valueOf(creator));
        List<AttachmentDto> attachments = industryDoc.getAttachments();
        if(attachments != null && attachments.size() > 0){
            String json = JSONArray.toJSONString(attachments);
            industryDoc.setAchment(json);
        }else{
            industryDoc.setAchment(null);
        }
        int insert = industryDocMapper.insert(industryDoc);
        directoryService.saveProAttachment(attachments, DirectoryTypeEnum.FILING_ITF_INDUSTRY.getCode(),industryDoc.getId(),String.valueOf(industryDoc.getValueid()),"");
        return insert;
    }

    @Override
    public Page<IndustryDoc> industryDocList(String industryId,String lvalueid,String lfileName,int page,int size) {
        QueryWrapper<IndustryDoc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("industry_id",industryId);
        queryWrapper.eq("delldetails","0");
        if (!StringUtils.isEmpty(lvalueid)){
            queryWrapper.eq("valueid",lvalueid);
        }
        if(!StringUtils.isEmpty(lfileName)){
            queryWrapper.eq("doc_name",lfileName);
        }
        if(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager())){
            queryWrapper.eq("creator",SecurityUtils.getCurrentUser().getId());
        }
        Page<IndustryDoc> pageIndustrytion = new Page<>(page, size);
        Page<IndustryDoc> industryPage = industryDocMapper.selectPage(pageIndustrytion, queryWrapper);
        List<IndustryDoc> industryDocs = industryPage.getRecords();
        industryDocs.forEach(industryDoc ->{
            String achment = industryDoc.getAchment();
            industryDoc.setAchment("");
            industryDoc.setAttachments(JSONObject.parseArray(achment, AttachmentDto.class));
        });
        return industryPage;
    }

    /**
     * 修改行业详情
     * @param industryDoc
     * @return
     */
    @Override
    public int updateIndusDoc(IndustryDoc industryDoc) {
        QueryWrapper<IndustryDoc> wrapper = new QueryWrapper<>();
        wrapper.eq("id",industryDoc.getId());
        List<AttachmentDto> attachments = industryDoc.getAttachments();
        if(attachments != null && attachments.size() > 0){
            String json = JSONArray.toJSONString(attachments);
            industryDoc.setAchment(json);
        }else{
            industryDoc.setAchment(null);
        }
        directoryService.saveProAttachment(attachments, DirectoryTypeEnum.FILING_ITF_INDUSTRY.getCode(),industryDoc.getId(),String.valueOf(industryDoc.getValueid()),"");
        int update = industryDocMapper.update(industryDoc, wrapper);
        return update;
    }

    @Override
    public int dellIndusDoc(String detailsId) {
        UpdateWrapper<IndustryDoc> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",detailsId);
        wrapper.set("delldetails","1");
        directoryService.removeAttachment(DirectoryTypeEnum.FILING_ITF_INDUSTRY.getCode(),detailsId);
        return industryDocMapper.update(null, wrapper);
    }
}
